
$(function(){
	
	get('system/label/getAll', function(data){
		if(data.success){
			new Vue({
				el: '#tabContent',
				data: data,
				methods: {
					clickBtn: function(txt){
						var words = $('#words').val();
						if(words.indexOf(txt)<0){
							if(isEmpty(words)){
								$('#words').val(txt);
							}else {
								$('#words').val(words+';'+txt);
							}
						}else {
							$.message({message:'“'+txt+'”已存在', type:'warning'});
						}
					},
					clickBtn1: function(txt){
						var resourceWords = $('#resourceWords').val();
						if(resourceWords.indexOf(txt)<0){
							if(isEmpty(resourceWords)){
								$('#resourceWords').val(txt);
							}else {
								$('#resourceWords').val(resourceWords+';'+txt);
							}
						}else {
							$.message({message:'“'+txt+'”已存在', type:'warning'});
						}
					},
					saveResource: function(){
						var resourceName = $('#resourceName').val();
						if(isEmpty(resourceName)){
							$.message({message:'请填写资源名称', type:'warning'});
							$('#resourceName').focus();
							return false;
						}
						
						var resourcePath = $('#resourcePath').val();
						if(isEmpty(resourcePath)){
							$.message({message:'请填写资源地址', type:'warning'});
							$('#resourcePath').focus();
							return false;
						}
						
						var resourceIntroduction = $('#resourceIntroduction').val();
						if(isEmpty(resourceIntroduction)){
							$.message({message:'请填写资源介绍', type:'warning'});
							$('#resourceIntroduction').focus();
							return false;
						}
						
						var resourceWords = $('#resourceWords').val();
						if(isEmpty(resourceWords)){
							$.message({message:'请编写搜索关键词', type:'warning'});
							$('#resourceWords').focus();
							return false;
						}
						
						var maxReadNum = $('#maxReadNum').val();
						if(!isEmpty(maxReadNum)){
							var reg = /^\d{1,10}$/;//10位以内的数字
							if (!reg.test(maxReadNum.trim())) {
								$.message({message:'请输入10位以内的正常数字', type:'warning'});
								$('#maxReadNum').focus();
								return false;
							}
						}
						
						put('system/resource/save', function(data){
							if(data.success){
								$.message({message:'资源保存成功', type:'success'});
								$('#resourceName').val('');
								$('#resourcePath').val('');
							}else {
								spop(data.result, 'error');
							}
						}, {
							resourceName: resourceName.trim(),
							resourcePath: resourcePath.trim(),
							resourceIntroduction: resourceIntroduction.trim(),
							searchKeyWords: resourceWords.trim(),
							maxReadNum: maxReadNum.trim()
						});
						
					},
					submitResource: function(){
						var resources = $('#resource').prop('files');
						if(resources==null || resources.length==0){
							$.message({message:'请选择要上传的资源', type:'warning'});
							$('#resource').focus();
							return false;
						}
						
						var introduction = $('#introduction').val();
						if(isEmpty(introduction)){
							$.message({message:'请填写资源介绍', type:'warning'});
							$('#introduction').focus();
							return false;
						}
						
						var words = $('#words').val();
						if(isEmpty(words)){
							$.message({message:'请编写搜索关键词', type:'warning'});
							$('#words').focus();
							return false;
						}
						
						var maxNum = $('#maxNum').val();
						if(!isEmpty(maxNum)){
							var reg = /^\d{1,10}$/;//10位以内的数字
							if (!reg.test(maxNum.trim())) {
								$.message({message:'请输入10位以内的正常数字', type:'warning'});
								$('#maxNum').focus();
								return false;
							}
						}
						
						var formData = new FormData();
						formData.append('resource', resources[0]);
						formData.append('introduction', introduction.trim());
						formData.append('words', words.trim());
						formData.append('maxReadNum', maxNum.trim());
						
						fetchRequest('POST', 'system/resource/saveResource', function(data){
							if(data.success){
								$.message({message:'保存成功', type:'success'});
								$('#resource').val('');
							}else{
								spop(data.result, 'error');
							}
						}, formData);
					}
					
				}
			});
		}else {
			spop(data.result, 'error');
		}
	});
	
});